package cs348a.opengl.object;

import java.awt.Color;
import java.util.List;

import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;

import cs348a.data.CurveEvaluator;
import cs348a.data.SequenceData;
import cs348a.data.Site;

public class OpenGLSequencePath extends AbstractOpenGLObject {
	/** The sequence data */
	private SequenceData sequenceData;
	
	/** The color of the path*/
	private Color pathColor = Color.RED;
	
	/** The curve evaluator */
	private CurveEvaluator ce = new CurveEvaluator();
	
	/** The number of segments per curve */
	private int segments = 10;
	
	public OpenGLSequencePath(SequenceData sd) {
		sequenceData = sd;
	}
	
	/**
	 * Sets the path color
	 * 
	 * @param c
	 */
	public void color(Color c) {
		pathColor = c;
	}
	
	@Override
	protected void drawComponent(GLAutoDrawable drawable) {
		GL gl = drawable.getGL();
		
		List<Site> siteList = sequenceData.siteList;
		
		int len = siteList.size();
		
		// draw edges
		for(int i = 0; i < len-1; i++) {
			
			if(siteList.get(i).isOnCurve) {
				// safety check points
				if(i+2 >= siteList.size()) return;
				
				Site s1 = siteList.get(i);
				Site s2 = siteList.get(i+1);
				Site s3 = siteList.get(i+2);
				
				ce.setControlPoints(s1, s2, s3);
				
				gl.glBegin(GL.GL_LINE_STRIP);
					for(int j = 0; j <= segments; j++) {
						gl.glColor3f(pathColor.getRed()/255f, 
								pathColor.getGreen()/255f, 
								pathColor.getBlue()/255f);
								
						float[] p = ce.evaluate(j/(float)segments);
						gl.glVertex3f(p[0], p[1], p[2]);
					}
				gl.glEnd();
			}
		}
	}

}
